﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HRM.Model
{
    /// <summary>
    /// Service dự án dùng để binding ra viewmodel
    /// </summary>
    public class ProjectService : IProjectService
    {
        /// <summary>
        /// Lấy tất cả các dự án trong hệ thống
        /// </summary>
        /// <returns>danh sách dự án</returns>
        public List<DuAn> GetByAll()
        {
            var list = new List<DuAn>();

            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in entities.DuAns)
                {
                    list.Add(item);
                }
            }

            return list;
        }

        public List<DuAn> SearchByProjectID(string id)
        {
            var list = new List<DuAn>();

            using (HRMEntities entities = new HRMEntities())
            {
                var query = from project in entities.DuAns where project.MaDuAn.Contains(id) select project;

                foreach (var item in query)
                {
                    list.Add(item);
                }
            }

            return list;
        }

        public List<DuAn> SearchByProjectName(string projectName)
        {
            var list = new List<DuAn>();

            using (HRMEntities entities = new HRMEntities())
            {
                var query = from project in entities.DuAns where project.TenDuAn.Contains(projectName) select project;

                foreach (var item in query)
                {
                    list.Add(item);
                }
            }

            return list;
        }


        public bool Update(string id, string name)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = (from p in entities.DuAns where p.MaDuAn == id select p).FirstOrDefault();

                if (query != null)
                {
                    query.TenDuAn = name;
                    entities.SaveChanges();
                    return true;
                }
                else
                {
                    MessageBox.Show("Không tồn tại mã dự án", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                    return false;
                }
            }
        }

        public bool Add(string id, string name)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = (from p in entities.DuAns where p.MaDuAn == id select p).FirstOrDefault();

                if (query != null)
                {
                    return false;
                }
                else
                {
                    entities.DuAns.Add(new DuAn(id, name));
                    entities.SaveChanges();
                    return true;
                }
            }
        }


        public bool Delete(DuAn da)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = (from p in entities.DuAns where p.MaDuAn == da.MaDuAn select p).FirstOrDefault();

                if (query != null)
                {
                    entities.DuAns.Remove(query);
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public List<DuAn> GetPaging(int pageIndex, int pageSize)
        {
            var list = new List<DuAn>();

            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.DuAns.OrderBy(p => p.MaDuAn).Skip((pageIndex - 1) * pageSize).Take(pageSize);

                foreach (var item in query)
                {
                    list.Add(item);
                }
            }

            return list;
        }


        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.DuAns.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }

        public bool DeleteList(List<DuAn> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in list)
                {
                    var pro = entities.DuAns.Where(x => x.MaDuAn == item.MaDuAn).First();
                    entities.DuAns.Remove(pro);
                }
                entities.SaveChanges();
                return true;
            }
        }

    }
}
